package cn.com.zhangwoo.microstar.homepage.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.microstar.activity.R;
import cn.com.zhangwoo.microstar.activity.MainActivity;
import cn.com.zhangwoo.microstar.adapter.ListCityAdapter;
import cn.com.zhangwoo.microstar.entity.CityUser;
import cn.com.zhangwoo.microstar.http.ApplicationController;
import cn.com.zhangwoo.microstar.http.NetUtils;
import cn.com.zhangwoo.microstar.http.NormalPostRequest;
import cn.com.zhangwoo.microstar.inteface.IUtilDB;
import cn.com.zhangwoo.microstar.utildb.AppManager;
import cn.com.zhangwoo.microstar.utildb.CommonToast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

/**
 * 城市
 * 
 * @author 张切
 * 
 */
public class CityUserActivity extends Activity implements OnItemClickListener,
		OnClickListener {

	private TextView mTextView;
	ListView mListView;
	Context context;
	int index = 1;
	private LinearLayout mLayout;
	private List<CityUser.Data> listCityUser;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppManager.getInstance().addActivity(CityUserActivity.this);
		if (null != getIntent()) {
			setContentView(R.layout.activity_city_list1);
			context = CityUserActivity.this;
			index = (getIntent().getIntExtra("position", 1) + 1);
			findViewById(R.id.signin_actionbar_title_img).setOnClickListener(
					this);
			mTextView = (TextView) findViewById(R.id.signin_actionbar_title_text);
			mTextView.setText("选择市区");
			mLayout=(LinearLayout) findViewById(R.id.loading_progress);
			initView();
			showPostRequest(index);
		}
	}

	private void initView() {
		mListView = (ListView) findViewById(R.id.city_list1_listView);
		mListView.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		if (listCityUser.size() > 0) {
			SharedPreferences.Editor editor = ApplicationController.editor;
			editor.putString("CityNAme", listCityUser.get(position).getName());
			editor.commit();
			// 1:双重返回
			// 2：跳转
			Intent intent = new Intent(CityUserActivity.this,
					MainActivity.class);
			/**
			 * 1：可以不要，用SharedPreferences
			 * 2：要时则多传一个值进判断，第一个Activity的跳转MainActivity时导致getIntent()也不为空
			 */
			// intent.putExtra("CityNAme",
			// listCityUser.get(position).getName());
			startActivity(intent);
			CityUserActivity.this.finish();
		}

	}

	public void showPostRequest(int index) {
		if (!NetUtils.isNetworkAvailable(CityUserActivity.this)) {
			Toast.makeText(CityUserActivity.this, "网络未连接", 2).show();
			return;
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("c", "district");
		map.put("a", "getlist");
		map.put("fid", String.valueOf(index));
		RequestQueue requestQueue = ApplicationController.getHttpQueue();
		Request<JSONObject> request = new NormalPostRequest(
				IUtilDB.MESSAGE_URL,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						Gson gson = new Gson();
						CityUser ll = gson.fromJson(response.toString(),
								CityUser.class);
						if (ll.getData().size() > 0) {
							showViewGone();
							mListView.setAdapter(new ListCityAdapter(
									ll.getData(), context));
							CommonToast.showToast(CityUserActivity.this, "数据加",
									1);
						}
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						CommonToast.showToast(CityUserActivity.this, "数据加载失败",
								1);
					}
				}, map);
		requestQueue.add(request);
	}

	private void showViewGone()
	{
		mLayout.setVisibility(View.GONE);
		mListView.setVisibility(View.VISIBLE);
	}
	@Override
	public void onClick(View v) {
		CityUserActivity.this.finish();

	}

}
